<?php

class GradeController extends BackendController
{

    public $layout='/layouts/column2';
    public $city_en;
    public function actionIndex()
    {
        header("Content-type: text/html; charset=utf-8");
        $model = new Grade();
        $criteria = new CDbCriteria;
        $criteria->order = "listorder DESC,id DESC";
        $dataProvider= new CActiveDataProvider($model, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        $this->render('index',array('dataProvider'=>$dataProvider));
    }
    public function isShow($data){
        $str = '';
        if($data->is_show==0){
            $str .= '<a onclick="setShow(\''.$data->id.'\')"  href="javascript:void(0);" style="color:red;" >设为显示</a>';
        }else{
            $str .= '<a style="color:green;"  onclick="setUnShow(\''.$data->id.'\')"  href="javascript:void(0);" >取消显示</a>';
        }
        return $str;

    }
    public function actionDelete(){
        $id = Yii::app()->request->getParam('id');
        $info = Grade::model()->findByPk($id);
        if($info){
            $ok = $info->delete();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '删除数据成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '删除数据失败';
            }

        }else{
            $data['status'] = 0;
            $data['message'] = '你无权作此操作';
        }
        echo json_encode($data);
        Yii::app()->end();
    }
    public function actionSetShow(){
        $id = Yii::app()->request->getParam('id');
        $info = Grade::model()->findByPk($id);
        if($info){
            $info->is_show = 1;
            $ok = $info->update();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '设为显示成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '设为显示失败';
            }
        }else{
            $data['status'] = 0;
            $data['message'] = '你无权作此操作';
        }
        echo json_encode($data);
        Yii::app()->end();
    }
    public function actionSetUnShow(){
        $id = Yii::app()->request->getParam('id');
        $info = Grade::model()->findByPk($id);
        if($info){
            $info->is_show = 0;
            $ok = $info->update();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '关闭显示成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '关闭显示失败';
            }
        }else{
            $data['status'] = 0;
            $data['message'] = '你无权作此操作';
        }
        echo json_encode($data);
        Yii::app()->end();
    }

    public function actionUpdate(){
        $id = Yii::app()->request->getParam('id');
        Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
        //获取区域
        $info = Grade::model()->findByPk($id);
        if(Yii::app()->request->isPostRequest){
            if($info){
                $info->setAttributes($_POST['Grade']);
                if($info->update()){
                    //写入周边
                    $data['status'] = 1;
                    $data['message'] = '年级修改成功';
                }else{
                    $data['status'] = 0;
                    $data['message'] = $this->showError($info->getErrors());
                }
            }else{
                $data['status'] = 0;
                $data['message'] = '你无权作此操作';
            }

            echo json_encode($data);
            Yii::app()->end();
        }
        $this->layout = '/layouts/dialog';
        $this->render('update',array('info'=>$info));
    }
    public function actionAdd(){
        if(Yii::app()->request->isPostRequest){
            $_POST['Grade']['create_time'] = time();
            $_POST['Grade']['is_show'] = 1;
            $model = new Grade();
            $model->setAttributes($_POST['Grade']);
            if($model->save()){
                $data['status'] = 1;
                $data['message'] = '发布年级成功';
            }else{
                $data['status'] = 0;
                $data['message'] = $this->showError($model->getErrors());
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
            //获取区域
            $this->layout = '/layouts/dialog';
            $this->render('add');
        }
    }


}